package edu.ntu.cloudroid.server;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.ntu.cloudroid.client.Generator;
import edu.ntu.cloudroid.model.IDs;
import edu.ntu.cloudroid.model.UserData;
import edu.ntu.cloudroid.model.data.VitalSignInfo;
import edu.ntu.cloudroid.model.friend.FriendInfo;
import edu.ntu.cloudroid.model.policy.PolicyInfo;
import edu.ntu.cloudroid.model.profile.ProfileInfo;

public class GenerateData extends HttpServlet {
	static final String DEFAULT_DATE = "2011-01-01 00:00";

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		PrintWriter pw = resp.getWriter();
		int i = 0;
		String nuserS = req.getParameter("nuser");
		if (null != nuserS) {
			int nuser = Integer.parseInt(nuserS);

			//Generate profiles and upload them
			List<UserData> profiles = Generator.generateProfiles(IDs.DataID.VitalSignData.ordinal(), nuser);

			pw.println("Profiles");
			for (UserData pi : profiles){
				try {
					Uploader.uploadProfile((ProfileInfo)pi);
					if (0 == 100 % ++i)
						pw.print(".");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			pw.println();
			pw.println("Done");
		}

		String doPolicyS = req.getParameter("p");
		if (null != doPolicyS) {
			int nuser = Integer.parseInt(doPolicyS);
		
		pw.println("Policies");
		List<UserData> policies = Generator.generatePolicies(0, nuser, false);
		for (UserData pi : policies){
			try {
				Uploader.uploadPolicy((PolicyInfo)pi);
				if (0 == 100 % ++i)
					pw.print(".");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		pw.println();
		pw.println("Done");
		pw.flush();
		}

		String friend = req.getParameter("f");
		
		if (null != friend) {
			pw.println("Friends");
			int nuser = Integer.parseInt(friend);
			List<FriendInfo> friends = Generator.generateFriends(nuser);
			for (FriendInfo fi : friends){
				try {
					Uploader.uploadFriend(fi);
					if (0 == 100 % ++i)
						pw.print(".");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			pw.println();
			pw.println("Done");
		}
		
		String simpleFriend = req.getParameter("sf");
		if (null != simpleFriend) {
			int nuser = Integer.parseInt(simpleFriend);
			pw.println("Friends (simple)");
			List<FriendInfo> friends = Generator.generateFriends(nuser, 0);
			for (FriendInfo fi : friends){
				try {
					Uploader.uploadFriend(fi);
					if (0 == 100 % ++i)
						pw.print(".");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			pw.println();
			pw.println("Done");
		}

		//pw.println("Policies");			
		//uploadGroupAndPolicies(1000, friends, com,false);
		//System.out.println("Done uploading groups");

		//System.out.println("Done uploading policies");
		//pw.println();
		//pw.println("Done");

		String doData = req.getParameter("d");
		if (null != doData) {
			int nuser = Integer.parseInt(doData);
		pw.println("Data");

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
		Date startingDate = null;
		try {
			startingDate = sdf.parse(DEFAULT_DATE);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		List<VitalSignInfo> locs = Generator.generateVitalSignData(nuser, startingDate, 10, 10);
		for (VitalSignInfo loc : locs)
			try {
				Uploader.uploadData(loc);
				if (0 == 100 % ++i)
					pw.print(".");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		pw.println();
		pw.println("Done");
		}
	}
}
